package dse.modules;

import java.util.*;

import testing.TestHelper;
import dse.DataObject;
import dse.DseException;
import dse.ExperimentConfiguration;

public class GeneralDataObjectGenerator implements DataObjectGenerator {
	//private Random random;
	private int numObjects;
	private int minSize, maxSize;
	private int shareCount;
	private int reqShares;
	
	public void initalize (
			ExperimentConfiguration systemSettings,
			Map<String, String> moduleSettings
			)
	throws DseException {
		//random = systemSettings.getRandom();
		
		ModuleUtil.SettingsReader sr = ModuleUtil.getSettingsReader(
				this.getClass(), moduleSettings
				);
		numObjects = sr.getInteger("num-objects");
		minSize = sr.getInteger("min-size");
		maxSize = sr.getInteger("max-size");
		shareCount = sr.getInteger("share-count");
		reqShares = sr.getInteger("req-share-count");
	}
	
	/**
	 * Generates all the data objects that will be used in the experiment.
	 * 
	 * @return	A collection of data objects.
	 */
	public Vector<DataObject> generateDataObjects () {
		Vector<DataObject> dos = new Vector<DataObject>(numObjects);
		//int minMaxDiff = maxSize - minSize;
		for (int a = 1; a <= numObjects; a++) {
			int size = minSize; //+ random.nextInt(minMaxDiff);
			dos.add(new DataObject (a,size,shareCount,reqShares)); 
		}
		return dos;
	}

	

	
	
	
	/**
	 * For testing :)
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main (String [] args) 
	throws Exception
	{
		if (args.length != 3 && args.length != 4) {
			System.out.println(
					"Usage: java -cp ... " +
					"dse.modules.GeneralDataObjectGenerator " +
					"<num-objs> <min-size> <max-size> (seed)"
					);
			System.exit(-1);
		}
		
		
		ExperimentConfiguration ss = TestHelper.createSystemSettings(args, 3);
		
		GeneralDataObjectGenerator gdog = new GeneralDataObjectGenerator ();
		
		Hashtable<String, String> props = new Hashtable<String, String>();
		props.put("num-objects", args[0]);
		props.put("min-size", args[1]);
		props.put("max-size", args[2]);
		props.put("share-count", "10");
		props.put("req-share-count", "5");
		
		gdog.initalize(ss, props);
		
		System.out.println("Creating data objects...");
		Vector<DataObject> dos = gdog.generateDataObjects();
		assert(dos.size() == gdog.numObjects);
		
		System.out.println("");
		double avgSize = 0;
		int minSize = gdog.maxSize;
		int maxSize = gdog.minSize;
		for (DataObject dobj : dos) {
			System.out.println("(" + dobj.id() + ", " + dobj.size() + ")");
			avgSize += dobj.size()/(double)dos.size();
			if (dobj.size() < minSize)
				minSize = dobj.size();
			if (dobj.size() > maxSize)
				maxSize = dobj.size();
		}
		System.out.println(
				"Min/Max Data Object Sizes: " + minSize + "/" + maxSize
				);
		System.out.println("Avg Data Object Size: " + avgSize);
		System.out.println("");
		System.out.println("Done.");
	}
	
}
